<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Front Controller
 *
 * This class provides a common place to handle any tasks that need to
 * be done for all public-facing controllers.
 *
 * @package    Bonfire\Core\Controllers
 * @category   Controllers
 * @author     Bonfire Dev Team
 * @link       http://guides.cibonfire.com/helpers/file_helpers.html
 *
 */
class Front_Controller extends Base_Controller
{
    protected $ip_address = "";
    protected $pager;

    //--------------------------------------------------------------------

    /**
     * Class constructor
     *
     */
    public function __construct()
    {
        parent::__construct();

        Events::trigger('before_front_controller');

        // Pagination config
        $this->pager = array(
            'full_tag_open'     => '<div class="dataTables_paginate"><ul class="pagination">',
            'full_tag_close'    => '</ul></div>',
            'next_link'         => '&rarr;', //'&rarr;', false
            'prev_link'         => '&larr;', //'&larr;', false
            'next_tag_open'     => '<li>',
            'next_tag_close'    => '</li>',
            'prev_tag_open'     => '<li>',
            'prev_tag_close'    => '</li>',
            'first_tag_open'    => '<li>',
            'first_tag_close'   => '</li>',
            'last_tag_open'     => '<li>',
            'last_tag_close'    => '</li>',
            'cur_tag_open'      => '<li class="active"><a href="#">',
            'cur_tag_close'     => '</a></li>',
            'num_tag_open'      => '<li>',
            'num_tag_close'     => '</li>',
//            'last_link'         => false,
//            'first_link'         => false,
            'use_page_numbers'  => false,
        );

        $this->load->library('template');
        $this->load->library('assets');

        $this->set_current_user();

        $this->ip_address = $this->input->ip_address();

        Events::trigger('after_front_controller');
    }//end __construct()

    /**
     * 浏览记录
     * @param  object  $building
     * @param  string  $channel    渠道
     * @param  integer $template   模板，暂时默认为1
     * @return mixed              返回两个浏览记录表中的id
     */
    public function viewBuildingLog($building, $channel = '', $template = 1) {
        $this->load->library('user_agent');
        $userAgent = $this->input->user_agent();
        if (empty($building) || $this->agent->is_robot || in_array($userAgent, ['YisouSpider', 'spider-ads'])) return false;
        $get = $this->input->get();
        $deviceID = isset($get['did']) ? trim($get['did']) : '';
        $didEncode = isset($get['encode']) ? trim($get['encode']) : 0;
        $smsID = isset($get['sms_id']) ? trim($get['sms_id']) : '';
        $requestID = isset($get['requestId']) ? trim($get['requestId']) : '';
        $this->load->library('Mobile-Detect/Mobile_Detect', null, 'mobile_Detect');
        $this->load->model('buildings/viewbuildinglog_model');
        $ip = $this->input->ip_address();
        // if ($ip == '127.0.0.1') return false;
        $cookie = $this->getLogCookie();
        // 楼盘访问记录
        //$viewBuildingLogId = get_view_log_id();
        // 来源搜索关键词
        $searchEngineData = get_search_engine_keyword($this->agent->referrer(), $this->agent->referrer());
        // 浏览设备类型 1pc 2mobile 3table 9robot
        $broswerType = ($this->mobile_Detect->isMobile() ? ($this->mobile_Detect->isTablet() ? 3 : 2) : 1);
        if ($this->agent->is_robot) {
            // 机器人
            $broswerType = '9';
        }
        $referrer = strlen($this->agent->referrer()) > 255 ? substr($this->agent->referrer(), 0, 255) : $this->agent->referrer();
        $visitPage = 'http://'.$_SERVER['HTTP_HOST'].urldecode($_SERVER['REQUEST_URI']);
        // 写日志
        $currentUniqid = md5(uniqid('', true));
        $uniKey = 'udf_view_uni_code';
        $uniVal = get_cookie($uniKey) ? : '';
        $parentUniqid = $channel != 'other' ? '' : $uniVal;
        set_cookie($uniKey, $currentUniqid, 60*30);

        $referrer = str_replace('#', '$', $referrer);
        $visitPage = str_replace('#', '$', $visitPage);
        $keywords = str_replace('#', '$', $searchEngineData['keywords']);
        $logStr = date("Y-m-d H:i:s")."#v#{$currentUniqid}#{$cookie}#{$requestID}#{$ip}#{$parentUniqid}#{$deviceID}#{$didEncode}#{$smsID}#{$this->agent->browser}#{$this->agent->platform}#{$keywords}#{$searchEngineData['search_engine']}#{$referrer}#{$visitPage}#{$building->dmp_building_code}#{$channel}#{$broswerType}#{$userAgent}#{$template}";
        view_event_log($logStr);
        return ['building_cookie_id'=>$cookie];
    }

    protected function getLogCookie() {
        $this->load->helper('cookie');
        $cookie = null;
        $name = 'udf_view_log';
        $cookieStr = get_cookie($name);
        if (empty($cookieStr)) {
            $cookie = uniqid().mt_rand(1,1000000);
            set_cookie($name, serialize(array('id'=>$cookie)), 60*60*24*365);
        } else {
            $cookieArr = unserialize($cookieStr);
            $cookie = $cookieArr['id'];
        }
        return $cookie;
    }

    //--------------------------------------------------------------------

}

/* End of file Front_Controller.php */
/* Location: ./application/core/Front_Controller.php */